package proxy

import (
	"awesomeProxy/system/helper"
	"errors"
	"fmt"
	"net"
	"time"
)

const ACTION_CONTROL int = 1
const ACTION_TRANSFORM int = 2
const ACTION_CREATE_WORK = 3
const ACTION_PING = 4
const SPLITE_CHAR = "*******"

type ControlMsg struct {
	Action   int
	UniqueId string
	ExtData  map[string]interface{}
}

type ResponseMsg struct {
	Action int
}

type ProxyConRelation struct {
	ConnChan1      chan []byte
	ConnChan2      chan []byte
	Conn1          net.Conn
	Conn2          net.Conn
	isStart        bool
	isError        bool
	isSetTimer     bool
	errorInfo      error
	WaitBuildTime  time.Duration
	TargetUniqueId string
}

func (proxyConRelation *ProxyConRelation) setTimer() {
	if proxyConRelation.isSetTimer == false {
		proxyConRelation.isSetTimer = true
		time.AfterFunc(proxyConRelation.WaitBuildTime, func() {
			if proxyConRelation.isStart == false {
				proxyConRelation.isError = true
				fmt.Println("等待建立连接超时")
				proxyConRelation.errorInfo = errors.New("等待建立连接超时")
			}
		})
	}
}
func (proxyConRelation *ProxyConRelation) setConn1(conn net.Conn) {
	proxyConRelation.setTimer()
	proxyConRelation.Conn1 = conn
	if proxyConRelation.Conn2 != nil && proxyConRelation.isStart == false {
		go proxyConRelation.Start()
	}
}

func (proxyConRelation *ProxyConRelation) setConn2(conn net.Conn) {
	proxyConRelation.setTimer()
	proxyConRelation.Conn2 = conn
	if proxyConRelation.Conn1 != nil && proxyConRelation.isStart == false {
		go proxyConRelation.Start()
	}
}
func (proxyClientConn *ProxyConRelation) Start() {
	if proxyClientConn.isStart == false {
		if proxyClientConn.Conn1 != nil && proxyClientConn.Conn2 != nil {
			proxyClientConn.isStart = true
			proxyClientConn.isError = false
		} else {
			return
		}
	}
	go func() {
		for proxyClientConn.isError == false {
			data := <-proxyClientConn.ConnChan1
			fmt.Println("正常接受到数据" + helper.Now())
			err := proxyClientConn.Conn2.SetWriteDeadline(time.Now().Add(time.Millisecond * 100))
			if err != nil {
				proxyClientConn.isError = true
				proxyClientConn.Conn2.Close()
				proxyClientConn.Conn1.Close()
				fmt.Println("对端连接关闭")
				proxyClientConn.errorInfo = errors.New("对端连接关闭")
				break
			}
			n, err := proxyClientConn.Conn2.Write(data)
			fmt.Println("写入数据成功" + helper.Now())
			if err != nil || n != len(data) {
				proxyClientConn.isError = true
				proxyClientConn.errorInfo = errors.New("对端连接关闭")
				fmt.Println("对端连接关闭")
				proxyClientConn.Conn1.Close()
				proxyClientConn.Conn2.Close()
				break
			}
		}
	}()
	go func() {
		for proxyClientConn.isError == false {
			data := <-proxyClientConn.ConnChan2
			fmt.Println("正常接受到数据" + helper.Now())
			err := proxyClientConn.Conn1.SetWriteDeadline(time.Now().Add(time.Millisecond * 100))
			if err != nil {
				proxyClientConn.isError = true
				proxyClientConn.Conn2.Close()
				proxyClientConn.Conn1.Close()
				fmt.Println("对端连接关闭")
				proxyClientConn.errorInfo = errors.New("对端连接关闭")
				break
			}
			n, err := proxyClientConn.Conn1.Write(data)
			fmt.Println("写入数据成功" + helper.Now())
			if err != nil || n != len(data) {
				proxyClientConn.isError = true
				proxyClientConn.Conn2.Close()
				proxyClientConn.Conn1.Close()
				fmt.Println("对端连接关闭")
				proxyClientConn.errorInfo = errors.New("对端连接关闭")
				break
			}
		}
	}()
}
func SetKeepAlive(conn *net.TCPConn) {
	err := conn.SetKeepAlive(true)
	if err != nil {
		return
	}
	err = conn.SetKeepAlivePeriod(time.Second * 3)
	if err != nil {
		return
	}
}
